import { ConfigEnv, loadEnv, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path';
import vueJsx from '@vue3-oop/plugin-vue-jsx'
import AutoImport from 'unplugin-auto-import/vite';
import { createHtmlPlugin } from 'vite-plugin-html'
import { viteMockServe } from "vite-plugin-mock";

const envDir = 'env';

export default ({command,mode}: ConfigEnv): UserConfig => {
  const env = loadEnv(mode,envDir);

  console.log(`command=${command}`);
  console.log(`mode=${mode}`);
  console.log(`env=${JSON.stringify(env)}`);

  return {
    server: {
      proxy: {
        "/api": {
          target: "http://127.0.0.1:8080",
          changeOrigin: true,
          // rewrite: (path) => path.replace(/^\/API/, ""),
        },
      },
    },
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        resolvers: [],
        dts: 'src/types/auto-import.d.ts',
        imports: ['vue','vue-router','@vueuse/core']
      }),
      createHtmlPlugin({
        entry: '/src/App.tsx',
        template: '/public/index.html',
        inject:{
          data:{
            title: '流程中心'
          }
        }
      }),
      viteMockServe({
        mockPath: "./mock",
        supportTs: false
      })
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, './src'),
      }
    },
    css: {
      // css模块化
      modules: {
        generateScopedName: '[name]_[local]_[hash:base64:5]',
        hashPrefix: 'prefix'
      },
      preprocessorOptions: {
        scss:{
          javascriptEnabled: true
        }
      }
    },
    build: {
      outDir: env.VITE_OUT_DIR,
    },
    base: env.VITE_BASE_URL,
    optimizeDeps: {
      exclude: [
        '@antv/x6-vue-shape',
        '@antv/x6-plugin-snapline',
        '@antv/x6-plugin-minimap',
        '@antv/x6-plugin-dnd',
        '@antv/x6-plugin-history',
        '@antv/x6-plugin-scroller',
        '@antv/x6-plugin-selection',
        '@antv/x6-plugin-export'
      ],
    },
  }
}
